package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 规格参数的分组表，每个商品分类下有多个规格参数组(SpecGroup)表控制层
 *
 * @author makejava
 * @since 2020-11-17 11:10:34
 */
@RestController
@RequestMapping("spec")
public class SpecController {
    /**
     * 服务对象
     */
    @Resource
    private SpecGroupService groupService;
    /**
     * 服务对象
     */
    @Resource
    private SpecParamService paramService;

    /**
     * 根据分类查询规格组
     * @param categoryId 分类id
     * @return 规格组的集合
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategory(@RequestParam("id") Long categoryId){
        // SELECT *  FROM tb_spec_group p WHERE p.category_id = 105
        List<SpecGroup> list = groupService.query().eq("category_id", categoryId).list();
        // 转换DTO
        List<SpecGroupDTO> dtoList = SpecGroupDTO.convertEntityList(list);
        // 返回
        return ResponseEntity.ok(dtoList);
    }

    /**
     * 查询规格参数
     * @param categoryId 分类id
     * @param groupId 规格组id
     * @param searching 是否搜索
     * @return 参数集合
     */
    @GetMapping("params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "groupId", required = false) Long groupId,
            @RequestParam(value = "searching", required = false) Boolean searching
    ){
        List<SpecParamDTO> list = paramService.querySpecParams(categoryId, groupId, searching);
        return ResponseEntity.ok(list);
    }

    /**
     * 根据分类id查询规格组及组内参数
     * @param categoryId 分类id
     * @return 组集合，及组内参数集合
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecList(@RequestParam("id") Long categoryId){
        List<SpecGroupDTO> list = groupService.querySpecList(categoryId);
        return ResponseEntity.ok(list);
    }
}